#Region - Header
;Api.au3
#endregion

#Region - _apiCall
; #FUNCTION# ====================================================================================================================
; Name...........: _callApi
; Description ...: Makes a call to GE Api
; Syntax.........: _callApi($url,$params,$action)
; Parameters ....: $url- String The URL to connect to (required)
;					$params - String Parameters to post to the servers (optional)
;					$action - The action that is used (default "GET")
; Return values .: Success - String Response Text
;				   Failure - 0 and Set @ERROR to:
;				   	|1 - Return status code was not 100
;					|2 - 
; ===============================================================================================================================
Func _apiCall($url,$params = 0,$action = "GET")
	Local $obj = ObjCreate("winhttp.winhttprequest.5.1")
	If $params Then $action = "POST"
	$obj.open($action,$url)
	$obj.setRequestHeader('GE-computerName',@ComputerName)
	$obj.setRequestHeader('GE-computerHash',$computerHash)
	debug(" * Action: " & $action)
	debug(" * Url   : " & $url)
	If $params Then
		If $action = "POST" Then $obj.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
		$obj.send($params)
		debug(" * Params: " & $params)
	Else
		$obj.send()
	EndIf
	Local $sData
	If $obj.Status = 200 Then
		$sData = $obj.responsetext
		debug("Returned Data: " & $sData)
		return $sData
	Else
		warn("Could not connect to " & $url & " with action " & $action & ". Response Status: " & $obj.Status)
		SetError(1,$obj.Status)
		Return 0
	EndIf
EndFunc
#EndRegion


Func _URLEncode($sData)
    ; Prog@ndy
    Local $aData = StringSplit(BinaryToString(StringToBinary($sData,4),1),"")
    Local $nChar
    $sData=""
    For $i = 1 To $aData[0]
        ;ConsoleWrite($aData[$i] & @CRLF)
        $nChar = Asc($aData[$i])
        Switch $nChar
            Case 45, 46, 48-57, 65 To 90, 95, 97 To 122, 126
                $sData &= $aData[$i]
            Case 32
                $sData &= "+"
            Case Else
                $sData &= "%" & Hex($nChar,2)
        EndSwitch
    Next
    Return $sData
EndFunc
